package com.xiaoyi.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.avin.yi.R;
import com.bumptech.glide.Glide;
import com.xiaoyi.adapter.ColorTypeAdapter;
import com.xiaoyi.entity.XyData;
import com.xiaoyi.tool.AvBaseTool;
import com.xiaoyi.tool.UrlTool;
import com.xiaoyi.ui.view.ColorFillImageView;

import java.util.Arrays;
import java.util.List;

import me.yokeyword.fragmentation.SupportActivity;


public class ColorFillActivity extends SupportActivity  {
    private ColorFillImageView colorFillImageView;
    private TextView currentValue ;
    private static final String KEY_BEAN = "ColorFillActivity" ;
    Context context ;
    String url ;
    public static void startInceTance(Context context   , XyData XyData){
        Intent intent = new Intent(context , ColorFillActivity.class) ;
        Bundle bundle = new Bundle();
        bundle.putSerializable( KEY_BEAN, XyData);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_colorfill);
        if(getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        context = this ;
        initView();
    }

    private void initView(){

        RecyclerView  recyclerview = findViewById(R.id.recyclerview) ;
        LinearLayoutManager manager = new LinearLayoutManager(this  );
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview.setLayoutManager(manager);
        ColorTypeAdapter  mAdapter = new ColorTypeAdapter(this);
        recyclerview.setAdapter(mAdapter);
        mAdapter.setDatas(getListData());
        mAdapter.setOnItemSelect(onItemSelect);
        colorFillImageView = findViewById(R.id.colorFillImageView) ;
        currentValue = findViewById(R.id.currentValue) ;


        Bundle bundle=getIntent().getExtras();
        XyData xyData= (XyData) bundle.getSerializable(KEY_BEAN);
        url=UrlTool.getDataLastUrl(xyData) ;

//        AvBaseTool.e("绘图路径="+url);
        Glide.with(this).load(url) .into(colorFillImageView);


        findViewById(R.id.goInto).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DoodleActivity.startInceTance(url , context);
            }
        });

    }



    private List<String>   getListData(){
        List<String> list =    Arrays.asList(
                "#f24057" ,
                "#f28440" ,
                "#f2c540" ,
                "#c2f240" ,
                "#88f240" ,
                "#40f2aa" ,
                "#40f2f0" ,
                "#40a0f2" ,
                "#4053f2" ,
                "#6240f2" ,
                "#a140f2" ,
                "#d640f2" ,
                "#FF0000" ,
                "#00FF00" ,
                "#0000FF"
                ) ;

        return list ;
    }


    private ColorTypeAdapter.OnItemSelect onItemSelect = new ColorTypeAdapter.OnItemSelect() {
        @Override
        public void onItemClick(int position, String colorStr) {
            colorFillImageView.setPaintColor(colorStr);
            currentValue.setBackgroundColor( Color.parseColor(colorStr) );
        }
    } ;




}


